import numpy as np

np.random.seed(seed=1234)

x = np.array(6)
print("x:", x)
print("x ndim:", x.ndim)
print("x shape:", x.shape)
print("x size:", x.size)
print("x dtype:", x.dtype)

x = np.array([1.3, 2.2, 1.7])
print("x:", x)
print("x ndim:", x.ndim)
print("x shape:", x.shape)
print("x size:", x.size)
print("x dtype:", x.dtype)

x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("x:\n", x)
print("x ndim:", x.ndim)
print("x shape:", x.shape)
print("x size:", x.size)
print("x dtype:", x.dtype)
# 函数
print("np.zeros((2,2)):\n", np.zeros((2, 2)))
print("np.ones((2,2)):\n", np.ones((2, 2)))
print("np.eye((2)):\n", np.eye((2)))
print("np.random.random((2,2)):\n", np.random.random((2, 2)))

# 索引（indexing）
x = np.array([1, 2, 3])
print("x[0]:", x[0])
x[0] = 0
print("x[0]:", x)

# 切片（slicing）
x = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(x)
print("x column 1: ", x[:, 1])
print("x row 0: ", x[0, :])
print("x rows 0,1,2 & cols 1,2: \n", x[:3, 1:3])

# 整数数组索引
print(x)
rows_to_get = np.arange(len(x))
print("rows_to_get", rows_to_get)
cols_to_get = np.array([0, 2, 1])
print("cols_to_get", cols_to_get)
print("indexed values", x[rows_to_get, cols_to_get])

# 布尔数组索引
x = np.array([[1, 2], [3, 4], [5, 6]])
print("x:\n", x)
print("x > 2:\n", x > 2)
print("x[x > 2]:\n", x[x > 2])

# 数学基础
x = np.array([[1, 2], [3, 4]], dtype=np.float64)
y = np.array([[1, 2], [3, 4]], dtype=np.float64)
print("x + y:\n", np.add(x, y))  # or x + y
print("x - y:\n", np.subtract(x, y))  # or x - y
print("x * y:\n", np.multiply(x, y))  # or x * y

# 点积
a = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)  # 我们可以指定dtype
b = np.array([[7, 8], [9, 10], [11, 12]], dtype=np.float64)
print(a.dot(b))

# 跨维度求和
x = np.array([[1, 2], [3, 4]])
print(x)
print("sum all: ", np.sum(x))  # 将所有元素相加
print("sum by col: ", np.sum(x, axis=0))  # 逐列将元素相加
print("sum by row: ", np.sum(x, axis=1))  # 逐行将元素相加

# 转置
print("x:\n", x)
print("x.T:\n", x.T)

# np.tile：重复维度
x = np.array([[1, 2], [3, 4]])
y = np.array([5, 6])
addent = np.tile(y, (len(x), 1))
print("addent: \n", addent)
z = x + addent
print("z:\n", z)

# 广播（broadcasting）
x = np.array([[1, 2], [3, 4]])
y = np.array([5, 6])
z = x + y
print("z:\n", z)



